package com.zkh.myutils.io.excel.opcpack;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * [Content_Types].xml 生成
 * @author zkh
 */
class ContentType {
	//固定结构
	private static String fixedStructures = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
			+ "<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">"
			+ "<Default Extension=\"rels\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/>"
			+ "<Default Extension=\"xml\" ContentType=\"application/xml\"/>"
			+ "<Override PartName=\"/xl/workbook.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml\"/>"
			+ "{worksheets}{themes}"
			+ "<Override PartName=\"/xl/styles.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml\"/>"
			+ "<Override PartName=\"/xl/sharedStrings.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml\"/>"
			+ "<Override PartName=\"/docProps/core.xml\" ContentType=\"application/vnd.openxmlformats-package.core-properties+xml\"/>"
			+ "<Override PartName=\"/docProps/app.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.extended-properties+xml\"/></Types>";
	
	/**
	 * 生成输入流
	 * @param sheetListSize sheet表数据
	 * @param sheetThemeSize sheet主题数量
	 * @return
	 */
	public static InputStream createInputStream(int sheetListSize, int sheetThemeSize){
		//sheet数据
		List<String> sheetList = new ArrayList<>();
		//遍历
		for(int i=0; i<sheetListSize; i++) {
			sheetList.add("<Override PartName=\"/xl/worksheets/sheet" + (i + 1) + ".xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml\"/>");
		}
		//主体
		List<String> themeList = new ArrayList<>();
		//遍历
		for(int i=0; i<sheetThemeSize; i++) {
			themeList.add("<Override PartName=\"/xl/theme/theme" + (i + 1) + ".xml\" ContentType=\"application/vnd.openxmlformats-officedocument.theme+xml\"/>");
		}
		//生成文本
		String txt = fixedStructures.replace("{worksheets}", String.join("", sheetList))
				.replace("{themes}", String.join("", themeList));
		//生成流
		return new ByteArrayInputStream(txt.getBytes());
	}
}
